# -*- coding: utf-8 -*-
"""
Created on Mon Dec  6 15:45:32 2021

@author: Administrator
"""

import pandas as pd
import numpy as np
import os
import sys

path = r'C:\Users\fengmingshan\Desktop\开通电联共享'
os.chdir(path)

sys.path.append(path)

from func_new import down_load_license, add_cu_gateway, add_cu_ip, add_cu_sctp, add_cu_mme
from func_new import active_feature, add_two_plmn, run_share_parameters, modity_tac,create_flex_counter
from func_new import add_external_enb, add_external_enb_ip, add_external_cell, add_relations, active_external_enb
from func_new import add_csfall_back2gran,add_csfall_back2wcmda

# 通过pci计算physicalLayerCellIdGroup 和 physicalLayerSubCellId的方法
# physicalLayerCellIdGroup  = math.floor(pci/3)
# physicalLayerSubCellId = pci%3

neighbor_file = '4G-4G邻区规划_新版.xlsx'  # 邻区规划表
externalENb_file = 'externalENodeBFunction.xlsx'  # 网管导出的外部基站表
share_file = '站址合并场景联通共享开通清单_爱立信.xlsx' # 需要开共享的基站清单
cs_fallback = '语音回落配置.xlsx'

df_neighbor = pd.read_excel(neighbor_file)

df_externalENb = pd.read_excel(externalENb_file)

df_share = pd.read_excel(share_file)

df_share_cell = pd.read_excel(share_file,sheet_name = '小区清单')

df_cs_fallback = pd.read_excel(cs_fallback)

externalENb_set = set(df_externalENb['eNBid'].unique()) # 外部基站的集合

#share_list = list(df_share['ENODEBID'].unique())  # 需要开放共享的基站list

for i in range(len(df_share)):  # 迭代需要开放共享的基站清单针对每个enb生成添加邻区指令
    enb = df_share.loc[i,'ENODEBID']

    add_cu_gateway(
        './{}.mos'.format(enb),
        df_share.loc[i,'联通网关'],
    )
    add_cu_ip(
        './{}.mos'.format(enb),
        df_share.loc[i,'联通IP'],
    )
    add_cu_sctp( './{}.mos'.format(enb))
    add_cu_mme( './{}.mos'.format(enb))
    active_feature( './{}.mos'.format(enb))
    add_two_plmn( './{}.mos'.format(enb))

    df_cell = df_share_cell[df_share_cell['ENODEBID']==enb]
    df_cell.reset_index(drop = True, inplace =True)
    for i in range(len(df_cell)):
        modity_tac(
            './{}.mos'.format(enb),
            df_cell.loc[i,'cellName'],
            df_cell.loc[i,'TAC'],
        )

    create_flex_counter(
        './{}.mos'.format(enb),
    )

    run_share_parameters(
        './{}.mos'.format(enb),
    )




    # 制作添加邻区脚本
    # 筛选目标小区为联通的邻区
    df_tmp = df_neighbor[df_neighbor['电信基站ID']== enb]
    df_tmp.reset_index(drop = True,inplace = True)

    if not df_tmp.empty: # 判断是否有要添加的邻区
        df_enb = df_tmp[['目标小区基站ID','业务IP','mnc']] # 取enb和S1IP两列数据，因为添加外部基站只用到这两列
        df_enb = df_enb[~df_enb['目标小区基站ID'].isin(externalENb_set)] # 筛选网管没有配置的外部基站
        df_enb.drop_duplicates('目标小区基站ID',inplace =True) # 去重
        df_enb.reset_index(drop = True,inplace = True) # 重排序号方便后续迭代
        if not df_enb.empty: # 添加外部基站和基站S1IP
            for i in range(len(df_enb)):
                add_external_enb(
                    './{}_neighbor.mos'.format(enb),
                    df_enb.loc[i,'mnc'],
                    df_enb.loc[i,'目标小区基站ID'],
                )
                add_external_enb_ip(
                    './{}_neighbor.mos'.format(enb),
                    df_enb.loc[i,'mnc'],
                    df_enb.loc[i,'目标小区基站ID'],
                    df_enb.loc[i,'业务IP'],
                )
        for i in range(len(df_tmp)):
            add_external_cell(
                './{}_neighbor.mos'.format(enb),
                df_tmp.loc[i,'mnc'],
                df_tmp.loc[i,'目标小区基站ID'],
                df_tmp.loc[i,'目标小区ID'],
                df_tmp.loc[i,'目标小区频点'],
                df_tmp.loc[i,'目标小区PCI'],
                df_tmp.loc[i,'目标小区TAC'],
            )

            add_relations(
                './{}_neighbor.mos'.format(enb),
                df_tmp.loc[i,'网管小区名'],
                df_tmp.loc[i,'mnc'],
                df_tmp.loc[i,'目标小区基站ID'],
                df_tmp.loc[i,'目标小区ID'],
                df_tmp.loc[i,'目标小区频点'],
            )
            active_external_enb(
                './{}_neighbor.mos'.format(enb),
                df_tmp.loc[i,'mnc'],
                df_tmp.loc[i,'目标小区基站ID'],
            )

    enbs = list(set(df_cs_fallback['enb'].unique()))
    for enb in enbs:
        df_tmp = df_cs_fallback[df_cs_fallback['enb']==enb]
        freq = int(df_tmp['频点'].unique()[0])

        if df_tmp['回落策略'].unique()[0] == '2G':
            with open('./{}_csfallback.mos'.format(enb),'a') as f:
                f.writelines('crn ENodeBFunction=1,GeraNetwork=1'+'\n')
                f.writelines('end'+'\n')
                f.writelines('\n')

                f.writelines('crn ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup={freq}'.format(freq = freq)+'\n')
                f.writelines('end'+'\n')
                f.writelines('\n')
            add_csfall_back2gran(
                    './{}_csfallback.mos'.format(enb),
                    freq,
                    df_tmp['cellname']
                    )
        else:
            with open('./{}_csfallback.mos'.format(enb),'a') as f:
                f.writelines('crn ENodeBFunction=1,UtraNetwork=1'+'\n')
                f.writelines('end'+'\n')
                f.writelines('crn ENodeBFunction=1,UtraNetwork=1,GeranFreqGroup={freq}'.format(freq = freq)+'\n')
                f.writelines('end'+'\n')
            add_csfall_back2wcmda(
                './{}_csfallback.mos'.format(enb),
                freq,
                df_tmp['cellname']
                )




